package com.blindnews.utils.impl;

import com.blindnews.pojo.News;
import com.blindnews.utils.NewsContent;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class NewsContentImpl implements NewsContent {

    @Override
    public String[] selectNetEasy(String url, String userAgent)  {
        try{
            Document detailDoc;
            String[] newsListArr;
            // 获取详情页数据
            detailDoc = Jsoup.connect(url).header("User-Agent", userAgent).get();
            System.out.println(detailDoc.text());
            // 获取新闻详情,select获取Post_body的所有子标签；
            Elements newsTitle = detailDoc.select("div.post_main>h1");//获得标题
            System.out.println("Title：" + newsTitle.text());
            Elements newsContent = detailDoc.select("div.post_body>*");//获得正文
            newsListArr = newsContent.text().split(" ");
            return newsListArr;
        }catch (IOException e) {
            e.printStackTrace();
        }
        return new String[0];
    }

    @Override
    public String[] selectZhongXin(String url, String userAgent)  {
        try{
            Document detailDoc;
            String[] newsListArr;
            // 获取详情页数据
            detailDoc = Jsoup.connect(url).header("User-Agent", userAgent).get();
            System.out.println(detailDoc.text());
            // 获取新闻详情,select获取Post_body的所有子标签；
            Elements newsTitle = detailDoc.select("div.content_maincontent_more>h1");//获得标题
            System.out.println("Title：" + newsTitle.text());
            Elements newsContent = detailDoc.select("div.content_maincontent_content>*");//获得正文
            newsListArr = newsContent.text().split(" ");
            return newsListArr;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new String[0];
    }

    @Override
    public String[] selectSouHu(String url, String userAgent) throws IOException {
        try{
            Document detailDoc;
            String[] newsListArr;
            // 获取详情页数据
            detailDoc = Jsoup.connect(url).header("User-Agent", userAgent).get();
            System.out.println(detailDoc.text());
            // 获取新闻详情,select获取Post_body的所有子标签；
            Elements newsTitle = detailDoc.select("div.text-title>h1");//获得标题
            System.out.println("Title：" + newsTitle.text());
            Elements newsContent = detailDoc.select("article.article >p");//获得正文
            newsListArr = newsContent.text().split(" ");
            return newsListArr;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new String[0];
    }


    private static int count = 0;

    @Override
    public List<News> getContent(List<News> newsListParam) throws IOException {
        List<News> newsList = newsListParam;
        // 设置请求头
        String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36";
        for (News news : newsList) {
            String[] newsListArr;
                if (news.getSource().equals("网易新闻")||news.getSource().equals("网易")) {
                    newsListArr = selectNetEasy(news.getLink(), userAgent);
                } else if (news.getSource().equals("中国新闻网")) {
//                中国新闻网，会遇到bug，返回的url会出现重复的片段，长度会增加20，通过长度判断，将重复的部分删除。
                    if (news.getLink().length() > 70) {
                        String replaceurl = news.getLink().replace("https://www.chinanews.comhttps://www.chinanews.com", "https://www.chinanews.com");
                        news.setLink(replaceurl);
                        newsListArr = selectZhongXin(replaceurl, userAgent);
                    } else {
                        newsListArr = selectZhongXin(news.getLink(), userAgent);
                    }
                } else if (news.getSource().equals("搜狐网")){
                    newsListArr = selectSouHu(news.getLink(), userAgent);
                }else {
                    continue;
                }
                StringBuilder stringBuilder = new StringBuilder();
                String content;
                for (String news2 : newsListArr) {
                    count += 1;
//                将空内容标签过滤掉
                    if (news2.isEmpty()) {
                        continue;
                    }
//                遇到延伸阅读，不再获取，跳出循环
                    if (news2.equals("延伸阅读")) {
                        break;
                    }
                    System.out.println("第" + count + "段");
                    System.out.println(news2);
                    stringBuilder.append(news2);
                }
                content = stringBuilder.toString();
                System.out.println(content);
                if(content.length()>12000){
                    news.setNewsContent("");
                }else {
                    news.setNewsContent(content);
                }
                if (news.getNewsContent().equals("")){
                    news.setDr("0");
                }else {
                    news.setDr("1");
                }
        }
        return newsList;
    }


}
